package zhieasy.cn.common.utils;

import java.io.*;

public class FileUtils {

    public static void copyInputStreamToFile(InputStream inputStream, File descFile) {
        try (FileOutputStream outputStream = new FileOutputStream(descFile)){
            int len;
            byte[] bytes = new byte[4096 * 2];
            while((len = inputStream.read(bytes)) != -1){
                outputStream.write(bytes, 0, len);
                outputStream.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void createFiles(File[] files){
        for (File file: files){
            File parentFile = file.getParentFile();
            if(!parentFile.exists()){
                parentFile.mkdirs();
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else{
                if(file.exists()) file.delete();
            }
        }
    }


}
